package bit.pack;

public class Test {

    public static Animal func2(int a){
        if(a == 1) {
            return new Dog2(6, "旺财");
        }else{
            return new Bird(8,"qiuqiu");
        }
    }   //向上转型的第三种方法，返回值传递


  public static void func(Animal animal){
        animal.eat();
  }

    public static void main(String[] args) {
//        Animal animal = new Dog2(8,"汪汪");
        Animal animal = new Bird(4,"JIUJIU");
        if(animal instanceof Dog2){
            Dog2 dog2 = (Dog2) animal;
            dog2.eat();
            System.out.println("animal引用的是Dog的对象");
        }else {
            System.out.println("animal引用的不是Dog的对象");
        }


        if (animal instanceof Bird){
            Bird bird = (Bird) animal;
            bird.eat();
            System.out.println("animal引用的是bird的对象");
        }else {
            System.out.println("animal引用的不是bird的对象");
        }


//        Dog2 dog = new Dog2();
//        Animal animal = dog;    //表示父类引用指向子类对象

//        Animal animal = new Dog2(18,"旺财");
//        animal.eat();     //向上转型的第一种,直接赋值

//        Dog2 dog2 = new Dog2(8,"旺财");
//        Bird bird = new Bird(9,"秋秋");
//        func(dog2);         //向上转型的第二种,参数传递
//        func(bird);         //同一个引用调用同一个方法,调用不同的对象时，产生的结果不一样，
//                            //这就叫做多态
//
//
//        Animal c = func2(1);
//        System.out.println(c); //在java中打印一个对象时要重写一个toString方法

    }

    public static void main1(String[] args) {
        Dog2 dog = new Dog2(18,"旺财");
        dog.eat();
    }
}
